package com.sunline.comm.protocal.http;

import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;

import com.nida.Nida;
import com.nida.mvc.RequestMethod;
import com.nida.route.RouteHandler;

import cn.sunline.rest.util.Convert;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;

public class HttpAcceptorHandler extends SimpleChannelInboundHandler<Object>{

    private Nida nida = Nida.me();
    
    @Override
    protected void messageReceived(ChannelHandlerContext ctx, Object msg) throws Exception {
        //判断请求方式
        if (msg instanceof FullHttpRequest) {
            //http请求
            
            FullHttpRequest request = (FullHttpRequest) msg;
            //request.
            //System.out.println();
            String uri = request.uri();
            if (!uri.startsWith(nida.getContextPath())) {
                RouteHandler.rend404(ctx, request);
                return;
            }
            String requestPath = uri.substring(nida.getContextPath().length());
            if ("".equals(requestPath)) {
                requestPath = "/";
            }
            RouteHandler handler = getRouteHandlerClass(requestPath, request);
            if (handler == null ) {
                RouteHandler.rend404(ctx, request);
                return;
            }
            executeRouteHandler(ctx,handler, request);
        } else if (msg instanceof WebSocketFrame) {
            //ws
            
        }
    }
    
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
    }
    
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
    
    private RouteHandler getRouteHandlerClass(String requestPath, FullHttpRequest request){
        RouteHandler handler = null;
        HttpMethod method = request.method();
        String requestType = method.name().toString();
        
        String key = requestPath + "#" + requestType;
        handler = nida.getRouters().getRoutes().get(key);
        if (handler == null) {
            key = requestPath + "#" +RequestMethod.ALL.toString();
            handler = nida.getRouters().getRoutes().get(key);
        }
        return handler;
    }
    
    private void executeRouteHandler(ChannelHandlerContext ctx,RouteHandler handler,FullHttpRequest request){
        FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1,HttpResponseStatus.OK, Convert.Obj2Buf(""));
        handler.excute(ctx, request, response);
    }
}
